%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

%%function connects all tracklets into tracks, first number in a track
%%represents the starting frame, the numbers after represent the connect
%%components in each frame starting from the starting frame, the last
%%number is -1 if track is incomplete and 0 if complete


function tracks = connect_tracklet(tracklet, cc)

%initalize tracklets
[m,~]=size(tracklet{1});
for i = 1 : m
    tracks(i).cc_label = tracklet{1}(i,:);
    tracks(i).start_frame = 1;
    tracks(i).complete = -1;
end

%after frame 1 connect tracklets
for i = 2 : length(tracklet)
    temp = tracklet{i}; %current frame of tracklets
    [m,~] = size(temp);
    not_new = [];
    
    %Compare all existing tracks with tracklets of current frame
    for j = 1 : length(tracks)
        
        %if track is incomplete
        if tracks(j).complete == -1
            %check if current track is connected in the current frame and mark
            %new tracks
            for k = 1 : m
                if tracks(j).cc_label(end) == temp(k,1)
                    tracks(j).cc_label(end + 1) = temp(k,2);
                    tracks(j).complete = -2;
                    not_new = [not_new; k]; %mark tracklet as a part of an old track
                    break
                end
            end
            
            %check if track was extended
            if tracks(j).complete == -2
                tracks(j).complete = -1;
            else
                tracks(j).complete = 0;
            end
        end     
    end  
    
    %Add new tracks from the unused tracklets
    temp(not_new,:) = [];
    
    if ~isempty(temp)
        k = 1;
        for j = ( length( tracks ) + 1 ) : ( length( tracks ) + length( temp( : , 1 )))
            tracks( j ).cc_label = temp( k, : );
            tracks( j ).start_frame = i;
            tracks( j ).complete = -1;
                  
            %check to see if new track split from an existing object
             tracks = check_split( i, j, k, tracks, cc, temp );
            
            k = k + 1;
            

        end
    end
    
    %check to see if any tracks merge
    tracks = check_merge( i, tracks, cc );
end
